package org.whb.pay.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.whb.basic.exception.BusinessException;
import org.whb.basic.service.impl.BaseServiceImpl;
import org.whb.order.service.IProductOrderService;
import org.whb.pay.constants.BussinessType;
import org.whb.pay.domain.AlipayInfo;
import org.whb.pay.domain.PayBill;
import org.whb.pay.mapper.AlipayInfoMapper;
import org.whb.pay.mapper.PayBillMapper;
import org.whb.pay.service.IPayBillService;
import org.whb.pay.utils.AlipayUtils;

import static java.awt.SystemColor.info;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill bill) {
        if (bill == null || bill.getId()==null){
            throw new BusinessException("支付单都不生成，你在调你马");
        }
        //防止别人使用postman来直接调用接口
        PayBill payBill = payBillMapper.loadById(bill.getId());
        if (payBill == null){
            throw new BusinessException("你的支付单有问题");
        }

        switch (payBill.getPayChannel().intValue()){
            case 1:{ //支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(bill, info);
            }
            case 2:{//微信
                return null;
            }
            case 3:{//银联
                return null;
            }
            default:break;
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
